package com.boot.test.utils;

import java.io.UnsupportedEncodingException;

/**
 * 密码工具
 * Created by wuxw on 2018/8/24.
 */
public class PasswordUtil {
    private static final int HASH_INTERATIONS = 64;
    private static final int SALT_SIZE = 8;

    /**
     * 生成随机salt
     */
    public static String generateSalt() {
        byte[] salt = Digests.generateSalt(SALT_SIZE);
        return Encodes.encodeHex(salt);
    }

    /**
     * 通过md5加密密码
     */
    public static String encryptPassword(String plainPassword, String salt) {
        byte[] hashPassword = new byte[0];
        try {
            hashPassword = Digests.md5(plainPassword.getBytes("UTF-8"), salt.getBytes("UTF-8"), HASH_INTERATIONS);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return Encodes.encodeHex(hashPassword);
    }


    /**
     * 设定安全的密码 sha-1 hash
     */
    public static String entryptPassword(String password) {
        byte[] hashPassword = Digests.sha1(password.getBytes());
        return Encodes.encodeHex(hashPassword);
    }
}
